package com.bdrm.service.impl;

import com.bdrm.constant.MessageConstant;
import com.bdrm.constant.StatusConstant;
import com.bdrm.dto.SetmealDTO;
import com.bdrm.dto.SetmealPageQueryDTO;
import com.bdrm.entity.Dish;
import com.bdrm.entity.Setmeal;
import com.bdrm.entity.SetmealDish;
import com.bdrm.exception.DeletionNotAllowedException;
import com.bdrm.mapper.DishMapper;
import com.bdrm.mapper.SetmealDishMapper;
import com.bdrm.mapper.SetmealMapper;
import com.bdrm.result.PageResult;
import com.bdrm.service.SetmealService;
import com.bdrm.vo.DishItemVO;
import com.bdrm.vo.SetmealVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //添加套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
//        setmeal.setStatus(StatusConstant.ENABLE);
        System.out.println("值"+setmeal);
        setmealMapper.insert(setmeal);
        System.out.println("到这一步了");
        Long setmealId =  setmeal.getId();
        System.out.println("setmealId"+setmealId);
        List<SetmealDish> setmealDish = setmealDTO.getSetmealDishes();
        if (setmealDish != null && setmealDish.size() > 0){
            setmealDish.forEach(setmealDishs -> {
                setmealDishs.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDish);
        }

    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        //查询套餐
        Setmeal setmeal = setmealMapper.getSetmealById(id);

        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(setmealId);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        Long setmealId = setmeal.getId();

        //先删除菜品
        setmealDishMapper.deleteBySetmealId(setmealId);

        //添加菜品
        List<SetmealDish> setmealDish = setmealDTO.getSetmealDishes();
        if (setmealDish != null && setmealDish.size() > 0){
            setmealDish.forEach(setmealDishs -> {
                setmealDishs.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDish);
        }
    }

    @Override
    public void deleteBach(List<Long> ids) {
        // 启用的不能删除
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getSetmealById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        // 删除套餐和套餐中的菜品
        ids.forEach(id -> {
            // 删除套餐
            setmealMapper.deleteById(id);
            // 删除套餐中的菜品
            setmealDishMapper.deleteBySetmealId(id);
        });
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        // 启用套餐之前先看套餐中的菜品是否为启用
        if (status == StatusConstant.ENABLE){
            List<Dish> dishList = dishMapper.getSetmealById(id);
            if (dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if (dish.getStatus() == StatusConstant.DISABLE){
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
